package com.dq.utils;

import com.alibaba.fastjson.JSONObject;

import com.aliyun.oss.ClientException;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.java.Log;

import javax.swing.text.html.FormSubmitEvent;
import java.rmi.ServerException;


/**
 * @author liuhui
 * @date 2019/9/22 0022 13:35:16
 *
 * 阿里大于的短信配置???   这里需要问一下三方资料    需要六个短信模板   一个短信签名
 */
@Log
public class SmsUtils {
    private static final String ACCESSKEYID = "LTAI4FhYADLb853Z1nTUMuCf";
    private static final String ACCESSSECRET = "D6QV7nI8vtJ4cBrml4JkQTIBmpIc4h";
    private static final String SIGNNAME = "\u5b89\u9ece\u5143";//安黎元
    private static final String BINDCARD_TEMPLATE_CODE = "SMS_174455281";//绑卡
    private static final String REGISTRY_TEMPLATE_CODE = "SMS_174455283";//注册
    private static final String FIND_CHECHOUT_STATUS_CODE = "SMS_174455286";//身份验证
    private static final String CHECKOUT_SUCCESS_CODE = "SMS_174989152";//审核通过通知SMS_174989152
    private static final String EDIT_PASSWORD_CODE = "SMS_174455282";//修改忘记密码
    private static final String EDIT_PHONE_CODE="SMS_174455282";//修改手机号

    /**
     * 绑卡
     *
     * @param phone
     * @param code
     * @return
     */
    public static boolean sendBindCardSms(String phone, String code) {
        return sendCN(phone, code, BINDCARD_TEMPLATE_CODE, "{\"code\":\"" + code + "\"}");
    }

    /**
     * 修改用户手机号发送验证码
     * @param phone
     * @param code
     * @return
     */
    public static boolean sendEditPhoneSms(String phone, String code){
        return sendCN(phone, code, EDIT_PHONE_CODE, "{\"code\":\"" + code + "\"}");
    }

    /**
     * 注册
     *
     * @param phone
     * @param code
     * @return
     */
    public static boolean sendRegistryCode(String phone, String code) {
        return sendCN(phone, code, REGISTRY_TEMPLATE_CODE, "{\"code\":\"" + code + "\"}");
    }

    /**
     * 查看审核状态
     *
     * @param phone
     * @param code
     * @return
     */
    public static boolean sendChechStatusCode(String phone, String code) {
        return sendCN(phone, code, FIND_CHECHOUT_STATUS_CODE, "{\"code\":\"" + code + "\"}");
    }

    /**
     * 商户
     *
     * @return
     */
    public static boolean checkOutSuccessCode(String localNickName, String username, String pwd) {
        String jsonCode = "{\"name\":\"" + localNickName + "\",\"username\":\"" + username + "\",\"password\":\"" + pwd + "\"}";
        return sendCN(username, jsonCode, CHECKOUT_SUCCESS_CODE, jsonCode);
    }


    /**
     * 商户-忘记密码
     *
     * @param phone
     * @param code
     * @return
     */
    public static boolean lostPwd(String phone, String code) {
        return sendCN(phone, code, EDIT_PASSWORD_CODE, "{\"code\":\"" + code + "\"}");
    }


    private static boolean sendCN(String phone, String code, String templateCode, String jsonCode) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESSKEYID, ACCESSSECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", SIGNNAME);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", jsonCode);
        try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject jsonObject = JSONObject.parseObject(response.getData());
            if ("OK".equalsIgnoreCase(jsonObject.getString("Code"))) {
                log.info(phone + ":短信发送成功,code:" + code);
                return true;
            }
            log.info("返回信息：" + response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
